package com.mrguo.service.impl.basedata.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrguo.common.dao.MyMapper;
import com.mrguo.common.entity.PageParams;
import com.mrguo.common.entity.Result;
import com.mrguo.common.exception.CustomsException;
import com.mrguo.common.service.BaseServiceImpl;
import com.mrguo.dao.bsd.StockMapper;
import com.mrguo.vo.basedata.StockOfSkuVo;
import com.mrguo.entity.goods.GoodsStock;
import com.mrguo.entity.sys.SysDataPermission;
import com.mrguo.entity.sys.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 郭成兴
 * @ClassName
 * @Description
 * @date 2020/1/410:41 PM
 * @updater 郭成兴
 * @updatedate 2020/1/410:41 PM
 */
@Service("stockServiceImpl")
public class GoodsStockServiceImpl extends BaseServiceImpl<GoodsStock> {

    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public MyMapper<GoodsStock> getMapper() {
        return stockMapper;
    }

    /**
     * 新增多条数据
     *
     * @param stockList
     * @return
     */
    void addListData(List<GoodsStock> stockList) throws CustomsException {
        if (stockMapper.insertList(stockList) < 0) {
            throw new CustomsException("批量插入库存失败！");
        }
    }

    /**
     * 查询库存列表
     *
     * @param pageParams
     * @return
     */
    public Result getStockList(PageParams<StockOfSkuVo> pageParams) {
        Page<StockOfSkuVo> page = pageParams.getPage();
        Map<String, Object> data = pageParams.getData();
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
        // 非管理员
        SysDataPermission dataPermission = userInfo.getDataPermission();
        page.setRecords(stockMapper.selectStocksPage(page, data));
        return Result.ok(page);
    }

    public GoodsStock getStockBySkuId(Long skuId) throws CustomsException {
        return stockMapper.getStockBySkuId(skuId);
    }

    public List<GoodsStock> listStockBySkuIds(List<Long> skuIds) {
        List<String> list = skuIds.stream().map(String::valueOf).collect(Collectors.toList());
        List<GoodsStock> goodsStocks = stockMapper.selectStockBySkuIds(list);
        if (goodsStocks.size() == 0) {
            // sku没有该仓库的信息，我们可以新增
            ArrayList<GoodsStock> goodsStocksParams = new ArrayList<>();
            for (Long skuId : skuIds) {
                GoodsStock goodsStock = new GoodsStock();
                goodsStock.setSkuId(skuId);
                goodsStock.setWaitQuantityIn(BigDecimal.ZERO);
                goodsStock.setWaitQuantityOut(BigDecimal.ZERO);
                goodsStock.setQuantityIn(BigDecimal.ZERO);
                goodsStock.setQuantityOut(BigDecimal.ZERO);
                goodsStocksParams.add(goodsStock);
            }
            stockMapper.insertList(goodsStocksParams);
            return goodsStocksParams;
        } else {
            return goodsStocks;
        }
    }
}
